package com.taomeng.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.taomeng.dao.DepartmentDao;
import com.taomeng.entity.Department;
import com.taomeng.entity.PageBean;
import com.taomeng.util.JdbcTemplateSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class DepartmentDaoImpl implements DepartmentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private JdbcTemplateSupport jdbcTemplateSupport;

    @Override
    public String findNameByClassId(Integer classId) {
        Object[] params = {classId};
        String sql = "select " +
                "d.name " +
                "from  " +
                "class c, " +
                "department d " +
                "where" +
                "c.departmentId=d.id " +
                " and " +
                " c.id=?";
        try {
            return jdbcTemplate.queryForObject(sql, params, String.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> findAll() {
        String sql = "select * from department";
        try {
            return jdbcTemplate.queryForList(sql);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageBean<Department> findByPage(Integer page, Integer limit) {
        String sql = "select * from department";
        if (null == page && null == limit) {
            try {
                List<Department> departmentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Department.class));
                PageBean<Department> pageBean = new PageBean<>();
                pageBean.setList(departmentList);
                pageBean.setPageSize(departmentList.size());
                return pageBean;
            } catch (DataAccessException e) {
                e.printStackTrace();
            }
            return null;
        }


        //这里使用了PageHelper插件，适配了JDBCTemplate
        Page<Department> pagination = PageHelper.startPage(page, limit);
        try {
            return jdbcTemplateSupport.queryForPage(sql, pagination, new BeanPropertyRowMapper<>(Department.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public PageBean<Department> findByPage(int page, int limit, String key) {
        String sql = "select * from department where name like ? or num like ? ";
        //这里使用了PageHelper插件，适配了JDBCTemplate
        Page<Department> pagination = PageHelper.startPage(page, limit);

        try {
            return jdbcTemplateSupport.queryForPage(sql, pagination, key, 2, new BeanPropertyRowMapper<>(Department.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public int addDepartment(Department department) {
        String sql = "insert into department values (?,?,?)";
        Object[] params = {
                department.getId(),
                department.getName(),
                department.getNum()
        };
        try {
            return jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delDepartmentById(int id) {
        String sql = "delete from department where id = ?";
        Object[] params = {
                id
        };
        try {
            return jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateDepartment(Department department) {
        String sql = "update department set name = ? , num = ? where id = ?";
        Object[] params = {
                department.getName(),
                department.getNum(),
                department.getId()
        };
        try {
            return jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean delBatchAll(String ids) {
        String sql = "delete from department where id = ?";

        String[] strings = ids.split(",");
        ArrayList<Object[]> list = new ArrayList<>();
        for (String id : strings) {
            Object[] o = {
                    id
            };
            list.add(o);
        }

        try {
            jdbcTemplate.batchUpdate(sql, list);
            return true;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

}
